package com.app.piemoment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.piemoment.callback.SizeCallBackForMenu;
import com.app.piemoment.database.SettingLoader;
import com.app.piemoment.view.MenuHorizontalScrollView;
import com.app.piemoment.view.PmDialog;

public abstract class SlideBaseActivity extends Activity implements OnClickListener {
	protected static final int LOAD_PROGRESS = 2012;
	protected int mainViewId;
	/**
	 * 侧滑菜单
	 */
	private LayoutInflater inflater;
	private View mainView;
	private View[] children;
	protected MenuHorizontalScrollView scrollView;
	private ImageView headicon;
	private TextView username;
	private ListView menuList;
	private LinearLayout btn_setting;
	// 顶部栏设置
	private Button btn_menu;
	private TextView title_text;
	private Button btn_right;
	private RelativeLayout btn_message;
	private LinearLayout btn_share;
	private LinearLayout btn_personal_home;
	private LinearLayout btn_pair_today;
	private LinearLayout btn_heart_test;
	private LinearLayout btn_personal_qa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		inflater = LayoutInflater.from(this);
		setContentView(inflater.inflate(R.layout.menu_scroll_view, null));

		this.mainView = inflater.inflate(mainViewId, null);

		View leftView = new View(this);
		leftView.setBackgroundColor(Color.TRANSPARENT);
		children = new View[] { leftView, mainView };
		scrollView = (MenuHorizontalScrollView) findViewById(R.id.mScrollView);
		btn_menu = (Button) this.mainView.findViewById(R.id.btn_left);
		title_text = (TextView) mainView.findViewById(R.id.text_title);
		btn_right = (Button) mainView.findViewById(R.id.btn_right);
		menuList = (ListView) findViewById(R.id.menuList);
		this.scrollView.initViews(children, new SizeCallBackForMenu(
				this.btn_menu), this.menuList);
		this.scrollView.setMenuBtn(this.btn_menu);
		btn_menu.setOnClickListener(this);
		initSlideMenus();
		registerButton();

		// 子页处理
		initMainView();
		registerMainViewEvent();
		processMainView();

		// TODO 用户头像和名字设置
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOAD_PROGRESS:
			PmDialog dialog = new PmDialog(this);
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}

	/**
	 * 更新title
	 * @param nickname
	 */
	public void updateTitle(String nickname) {
		if (title_text != null) {
			title_text.setText(nickname);
		}
	}
	
	
	protected void initTitle(int leftDrawable, int titleId, int rightDrawable,
			int rightText) {
		if (this.mainView == null) {
			this.mainView = inflater.inflate(mainViewId, null);
		}
		
		if (btn_menu != null) {
			if (0 != leftDrawable) {
				btn_menu.setBackgroundResource(leftDrawable);
			}
		}
		if (title_text != null) {
			if (0 != titleId) {
				title_text.setText(getResources().getString(titleId));
			}
		}

		if (btn_right != null) {
			if (0 != rightDrawable) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setBackgroundResource(rightDrawable);
			} else {
				btn_right.setVisibility(View.GONE);
			}
			if (rightText != 0) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setText(getResources().getString(rightText));
			}
			btn_right.setOnClickListener(this);
		}
	}

	protected void initTitle(int leftDrawable, String titleText, int rightId,
			int rightText) {
		if (this.mainView == null) {
			this.mainView = inflater.inflate(mainViewId, null);
		}
		if (btn_menu != null) {
			btn_menu.setBackgroundResource(leftDrawable);
		}
		if (title_text != null) {
			title_text.setText(titleText);
		}

		if (btn_right != null) {
			if (0 != rightId) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setBackgroundResource(rightId);
			} else {
				btn_right.setVisibility(View.GONE);
			}
			if (rightText != 0) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setText(getResources().getString(rightText));
			}
			btn_right.setOnClickListener(this);
		}
	}

	protected abstract void initMainView();

	protected abstract void registerMainViewEvent();

	protected abstract void processMainView();

	private void registerButton() {
		btn_personal_home.setOnClickListener(this);
		btn_pair_today.setOnClickListener(this);
		btn_heart_test.setOnClickListener(this);
		btn_personal_qa.setOnClickListener(this);
		btn_setting.setOnClickListener(this);
		btn_message.setOnClickListener(this);
		btn_share.setOnClickListener(this);
	}

	private void initSlideMenus() {
		headicon = (ImageView) findViewById(R.id.headicon);
		username = (TextView) findViewById(R.id.username);

		btn_personal_home = (LinearLayout) findViewById(R.id.btn_personal_home);
		btn_pair_today = (LinearLayout) findViewById(R.id.btn_pair_today);
		btn_heart_test = (LinearLayout) findViewById(R.id.btn_heart_test);
		btn_personal_qa = (LinearLayout) findViewById(R.id.btn_personal_qa);
		btn_setting = (LinearLayout) findViewById(R.id.btn_setting);
		
		btn_message = (RelativeLayout)findViewById(R.id.message_item);
		btn_share = (LinearLayout)findViewById(R.id.share_item);
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.message_item:
			intent = new Intent(this, NotifyActivity.class);
			startActivity(intent);
			break;
		case R.id.share_item:
			intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_TEXT,getResources().getString(R.string.help_text));
			startActivity(Intent.createChooser(intent, getTitle()));
			break;
		case R.id.btn_personal_home:
			intent = new Intent(this, PersonalActvitiy.class);
			startActivity(intent);
			break;
		case R.id.btn_pair_today:
			intent = new Intent(this, PairActivity.class);
			intent.putExtra("mid", SettingLoader.getMatchId(this)+"");
			startActivity(intent);
			break;
		case R.id.btn_heart_test:
			intent = new Intent(this, ModuleActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_personal_qa:
			// 显示答案 可供选择
			intent = new Intent(this, PersonalQuestionActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_setting:
			intent = new Intent(this, SettingActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}

	public void clickSlide() {
		scrollView.clickMenuBtn();
	}
}
